package com.hj.system.system.service.impl;

import com.hj.system.base.config.Constant;
import com.hj.system.base.domain.Tree;
import com.hj.system.base.utils.BuildTree;
import com.hj.system.system.dao.MenuDao;
import com.hj.system.system.dao.RoleDao;
import com.hj.system.system.domain.MenuDO;
import com.hj.system.system.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;


    @Override
    @Cacheable(value = "menus", key = "'menus'")
    public List<MenuDO> list(Map<String, Object> params) {
        List<MenuDO> menus = menuDao.list(params);
        MenuDO root = new MenuDO();
        root.setParentId("-1");
        root.setMenuId(Constant.MENU_ROOT_ID);
        root.setName(Constant.MENU_ROOT_NAME);
        menus.add(0, root);
        return menus;
    }

    @Override
    @CacheEvict(value = "menus", key = "'menus'")
    public int remove(String id) {
        int result = menuDao.remove(id);
        return result;
    }

    @Override
    @CacheEvict(value = "menus", key = "'menus'")
    public int save(MenuDO menu) {
        int r = menuDao.save(menu);
        return r;
    }

    @Override
    @CacheEvict(value = "menus", key = "'menus'")
    public int update(MenuDO menu) {
        int r = menuDao.update(menu);
        return r;
    }

    @Override
    public MenuDO get(String id) {
        MenuDO menuDO = menuDao.get(id);
        return menuDO;
    }

    /**
     * 根据roleId查询权限
     */
    @Override
    public Tree<MenuDO> getTree(String id) {
        List<MenuDO> menus = menuDao.list(new HashMap());
        List<String> menuIds = roleDao.listMenuIdByRoleId(id);
        List<String> temp = menuIds;
        for (MenuDO menu : menus) {
            if (temp.contains(menu.getParentId())) {
                menuIds.remove(menu.getParentId());
            }
        }
        List<Tree<MenuDO>> trees = new ArrayList();
        for (MenuDO sysMenuDO : menus) {
            Tree<MenuDO> tree = new Tree();
            tree.setId(sysMenuDO.getMenuId());
            tree.setParentId(sysMenuDO.getParentId());
            tree.setText(sysMenuDO.getName());
            Map<String, Object> state = new HashMap<>();
            String menuId = sysMenuDO.getMenuId();
            if (menuIds.contains(menuId)) {
                state.put("selected", true);
            } else {
                state.put("selected", false);
            }
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<MenuDO> t = BuildTree.build4Depth(trees, Constant.MENU_ROOT_ID, Constant.MENU_ROOT_NAME);
        return t;
    }

    @Override
    public Set<String> listPerms(String userId) {
        List<String> perms = menuDao.listUserPerms(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<MenuDO> listByMain(String type) {
        Map<String, String> param = new HashMap();
        param.put("type", type);
        List<MenuDO> menus = menuDao.listByMain(param);
        return menus;
    }

    /**
     * 根据用户id查询菜单
     */
    @Override
    @Cacheable(value = "menus", key = "'userId_'+#id")
    public List<Tree<MenuDO>> listMenuTree(String id) {
        List<Tree<MenuDO>> trees = new ArrayList<>();
        List<MenuDO> menuDOs = menuDao.listMenuByUserId(id);
        for (MenuDO sysMenuDO : menuDOs) {
            Tree<MenuDO> tree = new Tree<>();
            tree.setId(sysMenuDO.getMenuId());
            tree.setParentId(sysMenuDO.getParentId());
            tree.setText(sysMenuDO.getName());
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("url", sysMenuDO.getUrl());
            attributes.put("icon", sysMenuDO.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<MenuDO>> list = BuildTree.buildList4Depth(trees, "0");
        return list;
    }
}
